package com.jiepos.mpos.pay;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.biz.appInfM.service.MessageInfService;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.AppLogTransJnlsDao;
import com.jiepos.mpos.common.dao.AppPayDao;
import com.jiepos.mpos.common.dao.TransAmountCalDao;
import com.jiepos.mpos.common.service.AbrBaseHttpService;
import com.jiepos.mpos.core.util.StringUtils;

/**
 * 
 * 
 * <p>标题: —— 异步通知服务</p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @ProjectName:  [mpos] 
 * @Package com.jiepos.mpos.pay 
 * @author 张学涛
 * @version 1.0
 * @since 2017年10月17日下午3:22:28
 * @history（历次修订内容、修订人、修订时间等）
 */
@Service
public class NoticeService extends AbrBaseHttpService {
	private static final Logger _log = LoggerFactory.getLogger(NoticeService.class);
	
	@Autowired
	private MainBusinessService mainBusinessService;
	@Autowired
	private AppLogTransJnlsDao appLogTransJnlsDao;
	@Autowired
	private MessageInfService messageInfService;
	@Autowired
	private AppPayDao appPayDao;
	@Autowired
	private TransAmountCalDao transAmountCalDao;
	
	/**
	 * 更新流水和限额
	 * @param map
	 */
	public boolean noticeUpdate(Map<String,String> map){
		_log.info("流水号=[{}]，异步通知或主动查询标准化map=[{}]",map.get("loc_trace_no"), map);
		//异步通知更新流水
		boolean flag=false;	
		try{
			flag=appLogTransJnlsDao.updateByNotice(map);
			_log.info("流水号=[{}]，更新流水后的map=[{}]",map.get("loc_trace_no"), map);							
			if(SystemConstant.SUCCESS.equals(map.get("updateFlag"))){
				updateByResult(map);			
			}
		}catch(Exception e){
			flag=false;
			_log.error("异步通知或主动查询更新发生异常",e);				
		}				
		return flag;
	}
		
	/**
	 * 
	 * @param map
	 */
	public void updateByResult(Map<String,String> map){
		try{
			//计算分润
			calTransAmt(map);						
			//异步通知更新限额		
			mainBusinessService.updateLimitByNotice(String.valueOf(map.get("loc_dt")),String.valueOf(map.get("trans_at")),
					String.valueOf(map.get("trf_channel_id")),String.valueOf(map.get("trf_mchnt_cd")));
			//更新会员业务参数		
			mainBusinessService.updateCustomServiceParam(map);
			//充值成功之后，会员升级为VIP
			if(SystemConstant.SERVICE_NO_100002.equals(map.get("service_no"))){
				mainBusinessService.updateCustomVipSt(map);
			}
		}catch(Exception e){			
			_log.error("流水更新成功后相关更新发生异常",e);				
		}
	}
	
	/**
	 * 计算分润
	 * @param map
	 */
	public void calTransAmt(Map<String,String> map){
		try{
			String i_amount_type="";
			String amtType="";
			String serviceName="";
			String title="";
			if(SystemConstant.SERVICE_NO_100001.equals(map.get("service_no"))){
				i_amount_type="3";
				amtType="分润";
				serviceName="快捷支付";
				title="交易分润到账通知";
			}else if(SystemConstant.SERVICE_NO_100002.equals(map.get("service_no"))){
				i_amount_type="2";
				amtType="佣金";
				serviceName="充值";
				title="充值佣金到账通知";
			}
			Map<String,String> pushMap=transAmountCalDao.getTransAmountInfo(i_amount_type, null, map.get("loc_trace_no"));
			_log.info("流水号=[{}]，计算分润返回map=[{}]",map.get("loc_trace_no"), pushMap);	
			//消息推送
			if(pushMap!=null&&!pushMap.isEmpty()){			
				String o_direct_custom_uuid=pushMap.get("o_direct_custom_uuid");
				String o_indirect_custom_uuid=pushMap.get("o_indirect_custom_uuid");
				String o_direct_amount=pushMap.get("o_direct_amount");
				String o_indirect_amount=pushMap.get("o_indirect_amount");				
				StringBuffer textDirect=new StringBuffer();
				String hidePhone=StringUtils.hideData("2", pushMap.get("o_custom_phone"));
				textDirect.append("用户").append(hidePhone).append("做了一笔").append(serviceName);
				textDirect.append("业务，您获得了").append(o_direct_amount).append("元").append(amtType).append("！");
				StringBuffer textIndirect=new StringBuffer();
				textIndirect.append("用户").append(hidePhone).append("做了一笔").append(serviceName);
				textIndirect.append("业务，您获得了").append(o_indirect_amount).append("元").append(amtType).append("！");
							
				if(!StringUtils.isBlank(o_direct_custom_uuid)){
					messageInfService.pushMessage(o_direct_custom_uuid, null, title, textDirect.toString());				
				}
				if(!StringUtils.isBlank(o_indirect_custom_uuid)){
					messageInfService.pushMessage(o_indirect_custom_uuid, null, title, textIndirect.toString());				
				}					
			}
		}catch(Exception e){
			_log.error("计算分润和推送发生异常",e);	
		}
	}

}
